package com.music.server.controller;


import com.music.server.pojo.Singer;
import com.music.server.service.SingerService;
import com.music.server.utils.ResultVo;
import com.music.server.vo.SingerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 歌手 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-05
 */
@RestController
@RequestMapping("singer")
public class SingerController {

    @Autowired
    private SingerService singerService;

    /**
     * 查询指定的歌手信息
     * @param id
     * @return
     */
    @GetMapping("findById/{id}")
     public ResultVo findById(@PathVariable Integer id){
        return ResultVo.ok().data("singer",singerService.getById(id));
     }

   /**
     * 添加歌手信息
     * @return
     */
    @PostMapping("save")
    public ResultVo save(@RequestBody Singer singer){
        boolean save = singerService.save(singer);
        if (save) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 修改歌手信息
     * @return
     */
    @PutMapping("update")
    public ResultVo update(@RequestBody Singer singer){
        boolean update = singerService.updateById(singer);
        if (update) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 查询所有的歌手信息
     * @return
     */
    @GetMapping("list")
    public ResultVo list(){
        List<Singer> list = singerService.list();
        return ResultVo.ok().data("list",list);
    }

    /**
     * 分页条件查询所有的歌手信息
     * @return
     */
    @PostMapping("page/{pageNum}/{size}")
    public ResultVo page(@PathVariable Integer pageNum,@PathVariable Integer size,@RequestBody SingerVo singerVo){

        return ResultVo.ok().data("page",singerService.page(pageNum,size,singerVo));
    }


    /**
     * 删除歌手信息及对应的
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResultVo delete(@PathVariable Integer id){
        boolean delete = singerService.deleteById(id);
        if (delete) return ResultVo.ok();
        return ResultVo.error();
    }

}

